package com.yanqu.road.dao.impl.activity.jiuzhou.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouDeployData;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouRoomData;
import com.yanqu.road.entity.activity.jiuzhou.game.CrossJiuZhouGameData;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.mina.util.ConcurrentHashSet;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossJiuZhouRoomDataDaoImpl extends TempDataObjectDao<CrossJiuZhouRoomData> {
    
    public static String getCrossJiuZhouRoomDataSql = "select * from `t_c_jiu_zhou_room_data` where `activityId`=? ";

    public Map<Integer, CrossJiuZhouRoomData> getCrossJiuZhouRoomDataMap(int activityId) {
        Map<Integer, CrossJiuZhouRoomData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouRoomDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJiuZhouRoomData roomData = parse(rs);
                map.put(roomData.getRoomId(), roomData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossJiuZhouRoomDataSql = "update `t_c_jiu_zhou_room_data` set zone=?,round=?,state=?,roomUserIdList=?,gameData=?,watchUserIdList=? where activityId=? and roomId=? ";
    @Override
    protected boolean updateData(CrossJiuZhouRoomData crossJiuZhouRoomData) {
        crossJiuZhouRoomData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getZone()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getState()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouRoomData.getRoomUserIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossJiuZhouRoomData.getGameData() == null ? "" : JSON.toJSONString(crossJiuZhouRoomData.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouRoomData.getWatchUserIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getRoomId()));
        if (getDb().executeNoneQuery(updateCrossJiuZhouRoomDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossJiuZhouRoomDataSql = "insert into `t_c_jiu_zhou_room_data`(activityId,roomId,zone,round,state,roomUserIdList,gameData,watchUserIdList) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossJiuZhouRoomData crossJiuZhouRoomData) {
        crossJiuZhouRoomData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getRoomId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getZone()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouRoomData.getState()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouRoomData.getRoomUserIdList())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossJiuZhouRoomData.getGameData() == null ? "" : JSON.toJSONString(crossJiuZhouRoomData.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouRoomData.getWatchUserIdList())));
        if (getDb().executeNoneQuery(insertCrossJiuZhouRoomDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossJiuZhouRoomData parseData(ResultSet rs) throws SQLException {
        CrossJiuZhouRoomData data = new CrossJiuZhouRoomData();
        data.setActivityId(rs.getInt("activityId"));
        data.setRoomId(rs.getInt("roomId"));
        data.setZone(rs.getInt("zone"));
        data.setRound(rs.getInt("round"));
        data.setState(rs.getInt("state"));
        String roomUserIdList = rs.getString("roomUserIdList");
        if (!StringUtils.isNullOrEmpty(roomUserIdList)) {
            data.setRoomUserIdList(JSON.parseObject(roomUserIdList, new TypeReference<List<Long>>() {
            }));
        }
        String gameData = rs.getString("gameData");
        if (!StringUtils.isNullOrEmpty(gameData) && !gameData.equals("null")) {
            data.setGameData(JSON.parseObject(gameData, new TypeReference<CrossJiuZhouGameData>() {
            }));
        }
        String watchUserIdList = rs.getString("watchUserIdList");
        if (!StringUtils.isNullOrEmpty(watchUserIdList)) {
            data.setWatchUserIdList(JSON.parseObject(watchUserIdList, new TypeReference<ConcurrentHashSet<Long>>() {
            }));
        }
        return data;
    }
}
